package com.xlp;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Bookmark;
import org.apache.poi.hwpf.usermodel.Bookmarks;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;

import java.io.*;
import java.util.List;

public class WordUtil {
    public static void main(String[] args) throws Exception {
        FileInputStream inputStream = new FileInputStream("G:\\docTest\\123.doc");
        getBookmarksByDoc(inputStream);
        //inputStream.close();

       /* inputStream = new FileInputStream("G:\\docTest\\123.docx");
        getBookmarksByDocx(inputStream);
        inputStream.close();*/
    }
    /**
     * 读取 doc 文件中的所有书签
     *
     * @param inputStream
     * @throws IOException
     */
    public static void getBookmarksByDoc(InputStream inputStream) throws IOException {
        HWPFDocument wordDoc = new HWPFDocument(inputStream);

        Bookmarks bookmarks = wordDoc.getBookmarks();

        for (int b = 0; b < bookmarks.getBookmarksCount(); b++) {
            Bookmark bookmark = bookmarks.getBookmark(b);
            String bookMarkText = new Range(bookmark.getStart(), bookmark.getEnd(), wordDoc).text();

            System.out.println("[" + bookmark.getStart() + "; "
                    + bookmark.getEnd() + "]: " + bookmark.getName() + " = " + bookMarkText);

            Range range = new Range(bookmark.getStart(), bookmark.getEnd(), wordDoc);
            //range.replaceText("胡航", false);
            range.replaceText("送审251", false);
        }

        OutputStream outputStream = new FileOutputStream("G:\\docTest\\1234.doc");
        wordDoc.write(outputStream);
        outputStream.flush();
       // outputStream.close();
    }

    /**
     * 读取 docx 文件中的所有书签
     *
     * @param inputStream
     * @throws IOException
     */
    public static void getBookmarksByDocx(InputStream inputStream) throws IOException {
        XWPFDocument docx = new XWPFDocument(inputStream);

        List<XWPFParagraph> paragraphList = docx.getParagraphs();
        for (XWPFParagraph xwpfParagraph : paragraphList) {
            CTP ctp = xwpfParagraph.getCTP();
            System.out.println(12);
            for (int dwI = 0; dwI < ctp.sizeOfBookmarkStartArray(); dwI++) {
                CTBookmark bookmark = ctp.getBookmarkStartArray(dwI);
                String bookmarkName = bookmark.getName();

                // 因为 docx 的结构是xml格式，它不像 doc 文档那样有具体的start和end值来定位一个bookmark的值范围，
                // 所有如果你想要读取 docx 文档中bookmark的内容，你需要从 bookmarkStart 节点开始逐级逐层依次解析xml文
                // 件的 nodeValue 值并进行拼接，直至读取到下一个名为 bookmarkEnd 的节点为止。
                // 下面这个方法 getBookmarkTextContent() 是一个没有实现的方法，如有需要请自行实现
                // String bookmarkTextContent = getBookmarkTextContent(bookmark);

                System.out.println(bookmarkName);
            }
        }
    }
}
